<?php
class AttrController extends NodeController
{
    
    /**
     * 自定义标签
     * Enter description here .
     */
    public function flag()
    {
        if ($_POST) {
            $name = trim($_POST['name']);
            NewsModel::instance()->addFlag(array(
                    'name' => $name));
            $this->redirect(SUCCESS, '/news/flag');
            exit();
        }
        $this->view()->assign('cachetype', 0);
        $flag = NewsModel::instance()->getFlag();
        $this->view()->assign('flag', $flag);
        $this->view()->clearCache('file:news/flag.tpl');
        $this->view()->display('file:news/flag.tpl');
    }
    /**
     * 排序
     */
    public function s()
    {
        if (isset($_POST['sort']) && $_POST['sort'] >= 0) {
            $id = trim($_POST['sort']);
            NewsModel::instance()->setNewsSort($id, $_POST['id']);
            // print_r($aa);
            echo SUCCESS;
            // 清除缓存
            $this->view()->clearCache("file:news/list.tpl");
            exit();
        
     // $this->sendNotice ( "更新成功", null, true );
        } else {
            echo FAILED;
            exit();
        
     // $this->sendNotice ( "失败", null, true );
        }
    }
    /**
     * 多值字段
     */
    public function moreattr()
    {
        $rs = NewsModel::instance()->getExtend(array(
                'status' => 3));
        $this->view()->assign("cachetype", 1);
        $this->view()->assign("attr", $rs);
        $this->view()->clearCache("file:news/moreattr.tpl");
        $this->view()->display("file:news/moreattr.tpl");
    }
    /**
     * 自定义字段
     * Enter description here .
     */
    public function attr()
    {
        if ($_POST) {
            $key = trim(strtolower($_POST['key'])); //强制转化为小写
            $name = trim($_POST['name']);
            $flag = NewsModel::instance()->getExtend(array(
                    'key' => $key));
            if (count($flag) > 0) {
                $this->sendNotice('key already exsit', null, false);
            }
            $params = array(
                    'key' => trim($_POST['key']),
                    'name' => trim($_POST['name']),
                    'type' => trim($_POST['attribute']),
                    'status' => $_POST['status'],
                    'moduleid' => $_POST['module']);
            NewsModel::instance()->addExtend($params);
            $this->sendNotice(SUCCESS, null, true);
        }
        $this->view()->assign('cachetype', 0);
        $extend = NewsModel::instance()->getExtend();
        $this->view()->assign('extend', $extend);
        $this->view()->clearCache('file:news/attr.tpl');
        $this->view()->display("file:news/attr.tpl");
    }
    
    /**
     * 分类绑定字段
     */
    public function bindComment()
    {
        $config = require_once ROOT . '/config/base.php';
        $s = include ROOT . '/config/smarty.php';
        
        $this->view()->assign('cachetype', 1);
        $this->view()->assign('config', $config);
        $categoryid = isset($_GET['cid']) ? $_GET['cid'] : 0;
        if ($this->view()->isCached('file:news/cate.extend.tpl', $categoryid)) {
            $this->view()->assign('cachetype', 1);
            $this->view()->display('file:news/cate.extend.tpl', $categoryid);
            exit();
        }
        $this->view()->assign('cachetype', 0);
        $category = $this->getCategory($categoryid);
        $extend = NewsModel::instance()->getExtend(array(
                'module' => 'comment'));
        $cateInfo = NewsModel::instance()->getCateExtendByCid($categoryid);
        $selected = array();
        foreach ($cateInfo as $v) {
            array_push($selected, $v['eid']);
        }
        foreach ($extend as $k => $v) {
            if (in_array($v['eid'], $selected)) {
                $extend[$k]['checked'] = 'checked';
            }
        }
        $this->view()->assign('category', $category);
        $this->view()->assign('extend', $extend);
        $this->view()->display('file:news/comment.extend.tpl', $categoryid);
    }
}